{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.86448035]\n",
      " [-0.2555943 ]\n",
      " [-0.05587809]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,3,3],\n",
    "              [1,4,3],\n",
    "              [1,1,1],\n",
    "              [1,0,2]])\n",
    "#标签\n",
    "Y = np.array([[1],\n",
    "              [1],\n",
    "              [-1],\n",
    "              [-1]])\n",
    "\n",
    "#权值初始化，3行1列，取值范围-1到1\n",
    "W = (np.random.random([3,1])-0.5)*2\n",
    "\n",
    "print(W)\n",
    "#学习率设置\n",
    "lr = 0.11\n",
    "#神经网络输出\n",
    "O = 0\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,lr\n",
    "    O = np.sign(np.dot(X,W)) # shape:(3,1)\n",
    "    W_C = lr*(X.T.dot(Y-O))/int(X.shape[0])\n",
    "    W = W + W_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.75448035]\n",
      " [ 0.1294057 ]\n",
      " [ 0.27412191]]\n",
      "0\n",
      "Finished\n",
      "epoch: 0\n",
      "k= [-0.47207354]\n",
      "d= [2.75235329]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAHqR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zA81sipmVmNl8MxsSOlMymdluZjbDzObG3+/toTOlgplVM7PZZvZm6CzJZmafmtnHZjbHzIqS\n+lqZelnGzKoBi4ETgRXATKC/c25B0GBJZGZHA+uB551zh4XOk2xm1gBo4JybZWa/AYqB/4rq/8dm\nZsDuzrn1ZlYD+AAY4pz7KHC0pDKza4EYsIdzrlfoPMlkZp8CMedc0tf0Z/KZe2dgqXNumXNuIzAW\n6B04U1I5594Dvg6dI1Wcc6ucc7Pi338PlAANw6ZKHuetjz+sEf/KzLOvKjKzRkBP4OnQWaImk8u9\nIfD5Vo9XEOF/8LOdmTUBOgDTwyZJrvglijnAGmCycy7S7xd4EPgdUBY6SIo44G9mVmxm+cl8oUwu\nd6vguUif5WQrM6sLjAOuds59FzpPMjnnNjvn2gONgM5mFtnLb2bWC1jjnCsOnSWFujnnOgI9gCvi\nl1qTIpPLfQVw4FaPGwErA2WRJIlfex4HFDrnxofOkyrOuW+BqUD3wFGSqRtwWvw69FjgeDN7MWyk\n5HLOrYz/uQZ4HX95OSkyudxnAnlmdrCZ1QTOBiYEziQJFP+A8RmgxDn3QOg8yWZmuWa2V/z72sAJ\nwMKwqZLHOTfcOdfIOdcE/8/vu865gYFjJY2Z7R5fGICZ7Q6cBCRt5VvGlrtzbhNwJfA2/oO2V5xz\n88OmSi4z+xMwDWhuZivMbHDoTEnWDTgXf0Y3J/51SuhQSdQAmGJm8/AnL5Odc5FfHphF6gMfmNlc\nYAbwV+fcpGS9WMYuhRQRke3L2DN3ERHZPpW7iEgEqdxFRCJI5S4iEkEqdxGRCFK5i4hEkMpdRCSC\nVO4iIhH0v5gC6FQDnH2dAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1f4d996ae10>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in range(100):\n",
    "    update()#更新权值\n",
    "    print(W)#打印当前权值\n",
    "    print(i)#打印迭代次数\n",
    "    O = np.sign(np.dot(X,W))#计算当前输出  \n",
    "    if(O == Y).all(): #如果实际输出等于期望输出，模型收敛，循环结束\n",
    "        print('Finished')\n",
    "        print('epoch:',i)\n",
    "        break\n",
    "\n",
    "#正样本\n",
    "x1 = [3,4]\n",
    "y1 = [3,3]\n",
    "#负样本\n",
    "x2 = [1,0]\n",
    "y2 = [1,2]\n",
    "\n",
    "#计算分界线的斜率以及截距\n",
    "k = -W[1]/W[2]\n",
    "d = -W[0]/W[2]\n",
    "print('k=',k)\n",
    "print('d=',d)\n",
    "\n",
    "xdata = (0,5)\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(xdata,xdata*k+d,'r')\n",
    "plt.scatter(x1,y1,c='b')\n",
    "plt.scatter(x2,y2,c='y')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
